package onlinejudge.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import onlinejudge.entity.Notice;
import onlinejudge.service.NoticeService;
import onlinejudge.util.DateUtil;

@Controller
@RequestMapping("/notice")
public class NoticeController {

	@Resource
	private NoticeService noticeService;
	
	/**
	 * 获取所有公告列表接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/list")
	public String list(int page, int limit) {
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("page", (page-1)*limit);
		parameters.put("limit", limit);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("count", noticeService.getNoticeCount());
		json.put("data",noticeService.queryAllNotice(parameters));
		return json.toJSONString();
	}
	/**
	 * 公告删除接口
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestBody Map<String,Object> map) {
		Integer noticeId=(Integer) map.get("noticeId");
		noticeService.deleteNotice(noticeId);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data", "");
		return json.toJSONString();
	}
	/**
	 * 公告修改接口
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update")
	public String update(@RequestBody Map<String,Object> map) {
		Integer noticeId=(Integer) map.get("noticeId");
		String noticeContent=(String) map.get("noticeContent");
		String noticeDate=(String) map.get("noticeDate");
		Notice notice=new Notice(noticeId, noticeContent, noticeDate);
		System.out.println(notice);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data", "");
		return json.toJSONString();
	}
	/**
	 * 添加公告接口
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/add")
	public String add(@RequestBody Map<String,Object> map) {
		String noticeContent=(String) map.get("noticeContent");
		String noticeDate=DateUtil.getDate();
		Notice notice=new Notice();
		notice.setNoticeContent(noticeContent);
		notice.setNoticeDate(noticeDate);
		noticeService.releaseNotice(notice);
		JSONObject json=new JSONObject();
		json.put("msg","success");
		json.put("code","0");
		json.put("data", "");
		return json.toJSONString();
	}
}
